package com.cjm.algorithm.str;

import java.util.Stack;

public class Ti2405061047 {
    public static void main(String[] args) {
        Ti2405061047 ti2405061047 = new Ti2405061047();
        System.err.println(ti2405061047.removeDuplicates("abbaca"));
    }

    public String removeDuplicates(String s) {
        if (s == null || s.length() == 0 || s.length() == 1) return s;
        char[] charArray = s.toCharArray();
        Stack<Character> stack = new Stack<>();
        for (int i = 0; i < charArray.length; i++) {
            char c = charArray[i];
            if (!stack.isEmpty() && stack.peek() == c) {
                stack.pop();
            } else {
                stack.push(c);
            }
        }
        StringBuilder sb = new StringBuilder();
        while (stack.size() > 0) {
            sb.append(stack.pop());
        }
        return sb.reverse().toString();
    }
}
